#ifndef __CLIENT__
#define __CLIENT__
#include <my_header.h>
#include <syslog.h>
#include <mysql/mysql.h>
#include <openssl/sha.h>
typedef struct train_s{
    int flag;//新增加的
    int length;
    char data[1000];        
}train_t;

typedef  struct shareRes_s {
    char fileName[20];
    int fd;
} shareRes_t;

typedef struct Node_s {
    int data;
    char fileName[20];
    struct Node_s* next;
} Node_t;

// 定义队列结构体
typedef struct sonQueue_s {
    Node_t* front;  // 队列头
    Node_t* rear;   // 队列尾
} sonQueue_t;

int func_puts_client(int netfd, char * filename);
int func_gets_client(int netfd);
 char *file_sha1(const char *filename);
int recvn(int sockfd, void *buf, int length);
int recvn(int sockfd, void *buf, int length) {
    int total = 0;
    char *p = (char *)buf;
    while(total < length) {
        ssize_t sret = recv(sockfd, p+total, length-total, 0);
        total += sret;
    }
    return total;
}
// 打印日志
#define SYSLOG_PRINTF(type,message) \
    do{ \
        time_t now = time(NULL); \
        char *time_cur=ctime(&now);\
        size_t len=strlen(time_cur);\
        if(len>0&&time_cur[len-1]=='\n'){\
            time_cur[len-1]='\0';\
        }\
        syslog(type,"当前时间为:%s,当前操作为:%s\n",time_cur,message);\
    }while(0)\

int epollAdd(int epfd,int fd){
    struct epoll_event event;
    event.events=EPOLLIN;
    event.data.fd=fd;
    epoll_ctl(epfd,EPOLL_CTL_ADD,fd,&event);
    return 0;
}
int epollDel(int epfd,int fd){
    epoll_ctl(epfd,EPOLL_CTL_DEL,fd,NULL);
    return 0;    
}

// 初始化队列
void initQueue(sonQueue_t* Queue) {
    Queue->front = Queue->rear = NULL;
}

int isEmpty(sonQueue_t* Queue) {
    return Queue->front == NULL;
}
// 入队（尾插）
void enQueue(sonQueue_t* Queue, int value,char * f_n) {
    Node_t* newNode = (Node_t*)malloc(sizeof(newNode));
    if (!newNode) {
        printf("内存分配失败\n");
        return;
    }
    newNode->data = value;
    newNode->next = NULL;
    memcpy(newNode->fileName,f_n,strlen(f_n)+1);

    // 如果队列为空，新的节点既是头节点也是尾节点
    if (isEmpty(Queue)) {
        Queue->front = Queue->rear = newNode;
    } else {
        // 将新节点添加到队列的尾部
        Queue->rear->next = newNode;
        Queue->rear = newNode;
    }
}

// 出队（头删）
int deQueue(sonQueue_t* Queue) {
    if (isEmpty(Queue)) {
        printf("队列为空，无法出队\n");
        return -1; // 或者你可以选择返回一个特定的错误值
    }

    Node_t* temp = Queue->front;
    int value = temp->data;
    Queue->front = Queue->front->next;

    // 如果队列变为空，更新 rear 指针
    if (Queue->front == NULL) {
        Queue->rear = NULL;
    }

    free(temp); // 释放内存
    return value;
}


